import { ILoadMapTypeAll } from '@/common';
import { OneModal } from '@/components/one-ui';
import { getTenantID } from '@/utils';
import { Button, Col, DatePicker, Form, message, Row, Spin } from 'antd';
import moment from 'moment';
import { FC, useState, useEffect, useRef } from 'react';
import { ISourceIntentionAPI } from '../common/type';
import { PublishServices } from '../services';
import pageStyles from '@/common/pageLayout.less';
const PublishModal: React.FC<ISourceIntentionAPI.PublishModalAPI> = ({ onClose, publish }) => {
  /**弹框的ref*/
  const PublishModalRef = useRef(null);
  const [publishForm] = Form.useForm();
  const onCloseModal = () => {
    onClose();
  };
  // 回调函数
  const publishCallBack = (res: any) => {
    if (res?.msgFlag) {
      message.success(res?.msg);
      onCloseModal();
    }
  };
  // 发布意向公开时间方法
  const { publishRequest, loading } = PublishServices(publishCallBack);

  const disabledDate = (current) => {
    // 禁用当前日期之前的日期
    if (current && current.isBefore(moment().subtract(1, 'days').format('YYYY-MM-DD'))) {
      return true;
    }
    // 禁用2024-10-23之后的日期
    if (current && current.isAfter(moment(publish?.resObj?.expectTime, 'YYYY-MM-DD').add(1, 'days'))) {
      return true;
    }
    return false;
  };
  /**确定方法*/
  const onSave = () => {
    const { intentTime, announcementTime } = publishForm.getFieldsValue();
    if (!intentTime) {
      return message.error('请选择意向公开时间');
    }
    const params: any = {
      id: publish?.resObj?.id,
      state: `${getTenantID()}${ILoadMapTypeAll.DISCLOSURE_STATE}1005`,
      versionNum: publish?.resObj?.versionNum,
    };
    if (intentTime) {
      params.intentTime = moment(intentTime).format('YYYY-MM-DD HH:mm:ss');
      params.announcementTime = moment(announcementTime).format('YYYY-MM-DD HH:mm:ss');
    }
    publishRequest(params);
  };

  /**取消方法*/
  const onCancel = () => {
    onCloseModal();
  };

  /**点击确定*/
  const onConfirm = () => {
    onCloseModal();
  };
  // 定义一个状态变量来保存日期组件的值
  const [dateValue, setDateValue] = useState<any>(null);

  useEffect(() => {
    publishForm.setFieldsValue({ announcementTime: dateValue });
  }, [dateValue]);
  return (
    <OneModal
      open={true}
      getContainer={false}
      ref={PublishModalRef}
      title={'确认公布'}
      onCancel={onClose}
      onOk={onConfirm}
      width={400}
      bodyStyle={{
        padding: '5px 0px',
        height: '150px',
      }}
      footer={[
        <Button key="back" loading={loading} onClick={onCancel}>
          取消
        </Button>,
        <Button key="submit" loading={loading} type="primary" onClick={onSave}>
          确定
        </Button>,
      ]}
    >
      <Spin spinning={false}>
        <div className={pageStyles['fromView-styles']}>
          <Form form={publishForm}>
            <Form.Item
              label="意向公开时间"
              name="intentTime"
              rules={[
                {
                  required: true,
                  message: '请选择意向公开时间',
                },
              ]}
            >
              <DatePicker
                format={'YYYY-MM-DD'}
                // defaultValue={moment().add(1,'days').format('YYYY-MM-DD')}

                onChange={(value: any) => {
                  setDateValue(moment(value, 'YYYY-MM-DD').add(1, 'month'));
                }}
                disabledDate={disabledDate}
                // onFocus={(value:any)=>{
                //     setDateValue(moment(moment(value, 'YYYY-MM-DD').add(1, 'month')).format('YYYY-MM-DD'))
                // }}
              />
            </Form.Item>

            <Form.Item label="拟公告时间" name="announcementTime">
              <DatePicker value={dateValue} format={'YYYY-MM-DD'} disabled />
            </Form.Item>
          </Form>
        </div>
      </Spin>
    </OneModal>
  );
};
export default PublishModal;
